<?php
namespace Teacher\Controller;
use Common\Api\BookGameApi;
class ContestController extends TeacherBaseController {
	/**
	 * 比赛信息
	 *
	 * 		获取页面参数，
	 * 			通过教师id，比赛状态，多表查询到班级比赛信息。
	 */
	public function index(){
		$teacherId	=	session('user_auth.uid');
		$state		=	I('state');//比赛状态
		$book_game_name=I('book_game_name');
		$map['g.book_game_name']	=   array('like', '%'.(string)$book_game_name.'%');
		switch ($state){
			case 'underway'://进行中
				$map['g.isready']=2;
				$order='g.start_time desc';
				break;
			case 'over'://结束
				$map['g.isready']=0;
				$order='g.end_time desc';
				break;
		}
		$map['c.teacherid']	=	$teacherId;
		$map['g.status']	=	1;//状态（-1：已删除，0：禁用，1：正常）
		$classContestModel 	=	M('ClassInfo c')->join('primary_class_game b ON c.id = b.classid','LEFT')
								->join('primary_book_game g ON b.book_game_id = g.id','LEFT');//班级左连接比赛表
		$contestList   		= 	$this->lists($classContestModel, $map,$order,'g.*,b.classid');
		$this->assign('isready',$map['g.isready']);
		$this->assign('_list', $contestList);
		//Cookie('__forward__',$_SERVER['REQUEST_URI']);
		$this->display();
	}

	/**
	 * 查看比赛详细
	 *
	 * 		获取页面参数，
	 * 			通过比赛id查询比赛信息，
	 * 			通过班级id查询班级信息。
	 * 			通过比赛id,班级id，多表查询得到该系列内的书目及已读次数。
	 */
	public function detail(){
		$bookGameId		=	I('id');
		$classId		=	I('classid');
		//查询比赛信息
		$contest		=	M('BookGame')->find($bookGameId);
		//查询班级
		$classMap['id']	=	array('in',$contest['classids']);
		$class			=	M('ClassInfo')->where($classMap)->order('class_sno')->select();
		$class_name		=	array_column($class, 'class_name');
		$contest['class_names']	=	implode(',', $class_name);
		//查出图书
		$bookMap['id']	=	array('in',$contest['bookids']);
		$bookOfSeries	=	$this->lists('BookInfo',$bookMap);

		$this->assign('_list',$bookOfSeries);
		$this->assign('contest',$contest);
		$this->display();
	}

	/**
	 * 查看图书的阅读学生名单
	 */
	public function bookDetail(){
		$bookid		=	I('bookid');
		$classid	=	I('classid');
		$gameid		=	I('gameid');

		$bookGameStudentModel=M('BooklistGameRelation bgr')
						->join('primary_student s ON bgr.studentid=s.id AND s.classid='.$classid,'LEFT');
		$studentMap['bgr.book_game_id']	=	$gameid;
		$studentMap['bgr.bookid']		=	$bookid;
		$studentList=$this->lists($bookGameStudentModel,$studentMap,'s.sno','s.username');
		dump($studentList);
	}

	public function classStatistics(){
		$bookGameId=I('id');
		$classId=I('classid');
		$bookGameApi	=	new BookGameApi();
		$readOrNotData	=	$bookGameApi->getReadOrNotByGameIdAndClassId($bookGameId, $classId);
		$readTimeData	=	$bookGameApi->getReadTimeByGameIdAndClassId($bookGameId, $classId);
		$this->assign('ReadOrNot',$readOrNotData);
		$this->assign('ReadTime',$readTimeData);
		$this->display();
	}
	/**
	 * 学生统计
	 *
	 * 		获取页面参数，
	 * 			通过比赛id，查出比赛信息。
	 * 			通过比赛id，当前班级id，多表查询得到学生信息。
	 */
	public function statistics(){
		$username       		=   I('username');
		$map['s.sno|s.username']=	array('like', '%'.(string)$username.'%');

		$classId			=	I('classid');//获取页面的参数  班级id   查出学生信息
		$bookGameId			=	I('id');
		//查出比赛信息
		$contest			=	M('BookGame')->find($bookGameId);
		$contest['classid']	=	$classId;
		//查出学生信息
		$map['s.classid']	=	$classId;
		$studentContestModel=	M('Student s')->join('primary_book_game_relation bgr ON bgr.studentid=s.id and bgr.book_game_id ='.$bookGameId,'LEFT');//学生表左连接学生读书比赛关系表
		$studentList		=	$this->lists($studentContestModel,$map,'s.sno','s.username,s.sno,bgr.have_read_count,bgr.integral,s.id');

		$this->assign('contest',$contest);
		$this->assign('_list',$studentList);
		//Cookie('__forward__',$_SERVER['REQUEST_URI']);
		$this->display();
	}

	/**
	 * 查看学生详细
	 *
	 * 		获取页面参数，
	 * 			通过比赛id，查询比赛信息，
	 * 			通过班级id，查询班级信息，
	 * 			通过学生id，查询学生信息。
	 * 			通过学生id、比赛id，查询本次积分，已读图书；查询已读具体图书书目以及班级读该书次数。
	 *
	 */
	public function studentDetail(){
		// 		id classid studentid
		$bookGameId		=	I('id');
		$studentId		=	I('studentid');
		$classid		=	I('classid');
		//查询比赛信息
		$contest		=	M('BookGame')->field('book_game_name,isready')->find($bookGameId);
		//查询班级号
		$class_sno		=	M('ClassInfo')->where('id='.$classid)->getField('class_sno');
		$contest['class_sno']	=	$class_sno;
		//查询学生信息
		$student		=	M('Student')->field('sno,username')->find($studentId);
		$bookApi    = new BookGameApi();
		$readTimes  = $bookApi->getReadTimeByGameIdAndStudentId($bookGameId, $studentId);

		$stu['bgr.studentid']  =  $studentId;
		$stu['cgr.classid']    =  $classid;
		$stu['bgr.book_game_id']  =  $bookGameId;
		$bookList  =  M('BooklistGameRelation bgr')
					->join('primary_book_info bi ON bi.id=bgr.bookid','LEFT')
					->join('primary_class_game_relation cgr ON cgr.book_game_id=bgr.book_game_id and cgr.bookid=bgr.bookid')
					->where($stu)
					->field('bgr.*,bi.bookname,bi.cover,bi.intro,bi.author,bi.press,bi.book_page,cgr.read_times')
					->select();
		$this->assign('contest',$contest);
		$this->assign('student',$student);
		$this->assign('_list',$bookList);
		$this->assign('readtimes',$readTimes);
		$this->display();
	}
	
	public function getBookDetail(){
		$id			=	I('bookid');
		$bookModel	=	M('BookInfo');
		$book		=	$bookModel->find($id);
		$this->assign('book',$book);
		
		$this->assign('Book_Category',C("Book_Category"));
		$this->display();
	}
}